package com.flute.haflute.net.messaging;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * Mina FilterChain的一个filter，用于转换集群的消息为易传输对象<br>
 * 客户端和服务器端必须一致
 */
public class SinkFilterInMina extends IoFilterAdapter {

	@Override
	public void messageReceived(NextFilter nextFilter, IoSession session,
			Object message) throws Exception {
		if (message instanceof IoBuffer) {
			IoBuffer buffer = (IoBuffer) message;
			Message realMessage = Message.serializer().deserialize(
					new DataInputStream(
							new ByteArrayInputStream(buffer.array())));
			nextFilter.messageReceived(session, realMessage);
		} else {
			nextFilter.messageReceived(session, message);
		}
	}
}
